<?php
// +----------------------------------------------------------------------
// | zhanshop-docker-server / Ssh.php    [ 2023/12/18 11:52 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2023 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\controller;

use app\api\admin\Controller;
use zhanshop\App;
use zhanshop\server\Request;

class Ssh extends Controller
{
    /**
     * @var []
     */
    public $sshs = [];
    /**
     * @api TCP open/{tofd} 打开到设备终端
     * @apiGroup ssh终端
     * @param Request $request
     * @param \Swoole\Coroutine\Channel $response
     */
    public function open(Request $request, mixed $response){
        $tofd = $request->getData('tofd');
        $config = App::config()->get('bridge');
        $command = 'nohup php '.App::appPath().'/script/ssh.php '.$config['type'].' '.$config['ip'].' '.($config['port'] - 1).' '.$tofd;
        App::log()->push($command, "INFO");
        $log = App::runtimePath().DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR.'ssh.log';
        $command .= ' > '.$log.' 2>&1 &';
        \Swoole\Coroutine\System::exec($command);
    }
}